package cyou.breathe.blog.user.domain.service;

import com.alicp.jetcache.Cache;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import cyou.breathe.blog.user.domain.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * @author: breathe
 * @createTime: 2025-08-29
 */
@Service
public class UserCacheDelayDeleteService {

    private static final Logger LOG = LoggerFactory.getLogger(UserCacheDelayDeleteService.class);
    private static ThreadFactory userCacheDelayProcessFactory = new ThreadFactoryBuilder()
            .setNameFormat("user-cache-delay-process-pool-%d").build();

    private ScheduledExecutorService schedule = new ScheduledThreadPoolExecutor(10, userCacheDelayProcessFactory);

    public void delayCacheDelete(Cache idUserCache, User user) {
        schedule.schedule(() -> {
            boolean idDeleteResult = idUserCache.remove(user.getId().toString());

            LOG.info("idUserCache 第二次被移除了, key = {}, result = {}", user.getId(), idDeleteResult);
        }, 2, TimeUnit.SECONDS);
    }
}
